package com.lovestudying.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.entity.Course;
import com.lovestudying.entity.Teacher;
import com.lovestudying.service.CourseService;
import com.lovestudying.service.TeacherService;
import com.lovestudying.service.impl.CourseServiceImpl;
import com.lovestudying.service.impl.TeacherServiceImpl;
import com.lovestudying.util.PageInfo;
import com.oracle.jrockit.jfr.RequestableEvent;



/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/TeacherServlet")
public class TeacherServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       private TeacherService teacherService = new TeacherServiceImpl();
       private CourseService courseService = new CourseServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TeacherServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 接收请求中的参数op,请求操作标识
		String op = request.getParameter("op");
		
		if (op == null || "getTeacherList".equals(op)) {
			doGetList(request,response);
		}else if ("deleteByTeacherId".equals(op)) {
			deDeleteByTeacherId(request,response);
		}else if ("updateByTeacherId".equals(op)) {
			doUpdateByTeacherId(request,response);
		}
		
	}

	private void doUpdateByTeacherId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //	解析JSON字符串 - java对象	
		String json = request.getParameter("updateTeacher");
		Gson gson = new Gson();
		Teacher teacher = gson.fromJson(json, Teacher.class);
		teacherService.modifyById(teacher);
        // 响应 在一次转发		
		doGetList(request, response);
		
	}

	private void deDeleteByTeacherId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 调用模型 执行删除
		String teacher_id = request.getParameter("teacher_id");
		System.out.println(teacher_id);
		teacherService.deleteById(teacher_id);
		
		// 响应 在一次转发		
		doGetList(request, response);
		
	}

	private void doGetList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1.调用模型层,获取数据
				String teacherKey = request.getParameter("teacherKey");
//				System.out.println(teacherKey);
//				Map<String, String> params = new HashMap<String,String >();
//				params.put("teacherKey", teacherKey);
				List<Teacher> teacherList = teacherService.getTeacherListByCondition(teacherKey);
				Gson gson = new Gson();
				String jsonObject = gson.toJson(teacherList);
				response.setContentType("application/json");
				response.getWriter().write(jsonObject);
				
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
